package org.xtext.ic.generator;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;
import org.xtext.ic.generator.ModelExtensions;
import org.xtext.ic.test.Entity;
import org.xtext.ic.test.Model;
import org.xtext.ic.test.PackagePrefix;

@SuppressWarnings("all")
public class TestGenerator implements IGenerator {
  @Inject
  private IQualifiedNameProvider nameProvider;
  
  @Inject
  private ModelExtensions models;
  
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
    EList<EObject> _contents = resource.getContents();
    EObject _head = IterableExtensions.<EObject>head(_contents);
    Model m = ((Model) _head);
    PackagePrefix pp = m.getPackage();
    TreeIterator<EObject> _allContents = resource.getAllContents();
    Iterable<EObject> _iterable = IteratorExtensions.<EObject>toIterable(_allContents);
    Iterable<Entity> _filter = Iterables.<Entity>filter(_iterable, Entity.class);
    for (final Entity e : _filter) {
      {
        QualifiedName _fullyQualifiedName = this.nameProvider.getFullyQualifiedName(e);
        List<String> layerFileNames = _fullyQualifiedName.getSegments();
        String entityFileName = "";
        int i = 1;
        boolean _notEquals = (!Objects.equal(pp, null));
        if (_notEquals) {
          String _name = pp.getName();
          String[] prefixFileNames = _name.split("\\.");
          int j = 1;
          for (final String fileName : prefixFileNames) {
            {
              String _plus = (entityFileName + fileName);
              String _plus_1 = (_plus + "/");
              entityFileName = _plus_1;
              int _plus_2 = (j + 1);
              j = _plus_2;
            }
          }
        }
        for (final String fileName_1 : layerFileNames) {
          {
            int _size = layerFileNames.size();
            boolean _lessThan = (i < _size);
            if (_lessThan) {
              String _plus = (entityFileName + fileName_1);
              String _plus_1 = (_plus + "/");
              entityFileName = _plus_1;
            } else {
              String _plus_2 = (entityFileName + fileName_1);
              String _plus_3 = (_plus_2 + ".java");
              entityFileName = _plus_3;
            }
            int _plus_4 = (i + 1);
            i = _plus_4;
          }
        }
        CharSequence _compile = this.models.compile(m, e);
        fsa.generateFile(entityFileName, _compile);
      }
    }
  }
}
